package com.example.cookbook.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.widget.TextView;

import com.example.cookbook.NetUtil;
import com.example.cookbook.R;
import com.example.cookbook.adapter.RecipeSearchAdapter;
import com.example.cookbook.bean.RecipeBean;

import java.util.ArrayList;
import java.util.List;

public class MenuSearchActivity extends AppCompatActivity {
    private TextView txt_result;      //搜索结果布局中的提示信息控件
    private RecyclerView recy_view3;  //搜索结果布局中的列表控件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recy_cx_result);
        initview();
        loadData();

    }
    public void initview(){
        txt_result = findViewById(R.id.txt_result);
        recy_view3 = findViewById(R.id.recy_view3);  }
    private void loadData() {
        String ke_name=getIntent().getStringExtra("search");
        NetUtil.queryRecipe(ke_name, new NetUtil.OnRecipeQueriedCallback() {
            @Override
            public void onSuccess(final List<RecipeBean> recipes) {
                final List<RecipeBean> arrlist5 = new ArrayList<>(recipes);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txt_result.setText("您好，共查询到"+recipes.size()+"条数据，分别如下：");
                        RecipeSearchAdapter adapter=new RecipeSearchAdapter(MenuSearchActivity.this,arrlist5);
                        StaggeredGridLayoutManager st=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
                        recy_view3.setLayoutManager(st);
                        recy_view3.setAdapter(adapter);
                    }
                });
            }
            @Override
            public void onFailure(Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txt_result.setText("未查询到相关菜谱！"); }
                });
            }
        });
    }

}